package cn.jjxx.core.tags.form;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;

import org.springframework.web.servlet.tags.form.TagWriter;

import cn.jjxx.core.tags.html.manager.HtmlComponentManager;
import cn.jjxx.core.utils.SpringContextHolder;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.sys.tags.SysFunctions;

@SuppressWarnings("serial")
public class WebUploaderTag extends HiddenInputTag{

	protected HtmlComponentManager htmlComponentManager = SpringContextHolder.getApplicationContext()
			.getBean(HtmlComponentManager.class);
	
	protected String webUploadType = "webupload"; // 文本上传类型
	protected String uploadUrl = null;//上传地址
	protected String delUrl = null;//删除地址
	protected String mergeCheckUrl = null;//合并地址
	protected String upProgressUrl = null;//进度地址
	protected String selectFileType = "normal";//选择文件方式（拖拽(drag)、裁剪(cropper)、正常(normal)）	
	protected Boolean disableGlobalDnd = Boolean.FALSE;// 是否禁掉整个页面的拖拽功能
	protected String path = "";//选择文件的按钮容器
	protected String pickLabel = "选择文件";//指定按钮文字，采用 innerHTML 代替
	protected String innerHTML = "选择文件";//指定按钮文字
	protected Boolean multiple = Boolean.TRUE;//开启同时选择多个文件
	protected String title = "文件上传";//接收文件标题
	protected String extensions;//允许上传文件扩展名
	protected String mimeTypes;//上传文件的类型
	protected Boolean auto;//是否自动上传
	protected String runtimeOrder = "html5,flash";//指定运行时启动顺序
	protected Boolean prepareNextFile = Boolean.FALSE;
	protected Boolean chunked;//是否分片上传
	protected int chunkSize = 5242880;//分段上传大小
	protected int chunkRetry = 2;//网络问题，允许重传次数
	protected int threads = 3;//上传并发数
	protected String formData = "{}";//上传表单参数
	protected String fileVal = "file";//文件上传域的name
	protected String method = "POST";//上传方式，可选POST/GET
	protected Boolean sendAsBinary =  Boolean.FALSE;//是否二进制流发送
	protected int fileNumLimit;//允许最大上传文件数
	protected int fileSizeLimit;//允许总文件上传的size
	protected int fileSingleSizeLimit;//允许单个文件的上传的size
	protected Boolean duplicate ;//去掉相同的文件
	protected String disableWidgets;// 加载Uploader.register的 widget 
	protected String thumb;//配置生成缩略图的选项
	protected String compress ;//配置压缩图片选项
	
	protected String idField = "id";
	protected String filepathField = "filepath";
	
	protected String tableName;//上传所用的表名
	protected String tableFeildName;//上传所用的字段名
	protected String webName;//上传所用的服务名
	protected Boolean formUpload = Boolean.FALSE;//表单上传
	
	//事件
	protected String dndAccept;//拖拽时阻止事件
	protected String filesQueued;//当一批文件添加进队列以后触发
	protected String beforeFileQueued;//文件被加入队列之前触发
	protected String uploadSuccess;//当文件上传成功时触发
	protected String uploadError;//当文件上传出错时触发
	protected String uploadComplete;//文件上传完成时触发
	protected String uploadStart;//文件开始上传前触发
	protected String uploadBeforeSend;//文件分块上传时需要附带的参数
	protected String uploadAccept;//上传接收文件
	
	
	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		tagWriter.startTag("input");
		writeDefaultAttributes(tagWriter);
		tagWriter.writeAttribute("type", "hidden");
		if (isDisabled()) {
			tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled");
		}
		String value = getDisplayString(getBoundValue(), getPropertyEditor());
		tagWriter.writeAttribute("value", processFieldValue(getName(), value, "hidden"));
		tagWriter.endTag();
		// 输出编辑器代码片段
		writeFragment();
		return SKIP_BODY;
	}
	
	private void writeFragment() throws JspException {
		
		Map<String, Object> rootMap = new HashMap<String, Object>();
		String ctx = pageContext.getServletContext().getContextPath();
		String adminPath = pageContext.getServletContext().getContextPath() + SysFunctions.getAdminUrlPrefix();
		String staticPath = pageContext.getServletContext().getContextPath() + "/static";
		rootMap.put("ctx", ctx);
		rootMap.put("adminPath", adminPath);
		rootMap.put("staticPath", staticPath);
		String fragment = htmlComponentManager.getFragmentComponent(getComponentKey(), rootMap);
		if (!StringUtils.isEmpty(fragment) && !fragment.equals("null")) {
			// 获得编辑器
			try {
				super.pageContext.getOut().write(fragment);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	private String getComponentKey() {
		return webUploadType + "-file";
	}
	
	public HtmlComponentManager getHtmlComponentManager() {
		return htmlComponentManager;
	}
	public void setHtmlComponentManager(HtmlComponentManager htmlComponentManager) {
		this.htmlComponentManager = htmlComponentManager;
	}
	
	public String getUploadUrl() {
		return uploadUrl;
	}

	public void setUploadUrl(String uploadUrl) {
		this.uploadUrl = uploadUrl;
	}

	public String getDelUrl() {
		return delUrl;
	}

	public void setDelUrl(String delUrl) {
		this.delUrl = delUrl;
	}

	public String getMergeCheckUrl() {
		return mergeCheckUrl;
	}

	public void setMergeCheckUrl(String mergeCheckUrl) {
		this.mergeCheckUrl = mergeCheckUrl;
	}

	public String getUpProgressUrl() {
		return upProgressUrl;
	}

	public void setUpProgressUrl(String upProgressUrl) {
		this.upProgressUrl = upProgressUrl;
	}

	public Boolean getDisableGlobalDnd() {
		return disableGlobalDnd;
	}
	public void setDisableGlobalDnd(Boolean disableGlobalDnd) {
		this.disableGlobalDnd = disableGlobalDnd;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public String getPickLabel() {
		return pickLabel;
	}
	public void setPickLabel(String pickLabel) {
		this.pickLabel = pickLabel;
	}
	public String getInnerHTML() {
		return innerHTML;
	}
	public void setInnerHTML(String innerHTML) {
		this.innerHTML = innerHTML;
	}
	public Boolean getMultiple() {
		return multiple;
	}
	public void setMultiple(Boolean multiple) {
		this.multiple = multiple;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getExtensions() {
		return extensions;
	}
	public void setExtensions(String extensions) {
		this.extensions = extensions;
	}
	public String getMimeTypes() {
		return mimeTypes;
	}
	public void setMimeTypes(String mimeTypes) {
		this.mimeTypes = mimeTypes;
	}
	public Boolean getAuto() {
		return auto;
	}
	public void setAuto(Boolean auto) {
		this.auto = auto;
	}
	public String getRuntimeOrder() {
		return runtimeOrder;
	}
	public void setRuntimeOrder(String runtimeOrder) {
		this.runtimeOrder = runtimeOrder;
	}
	public Boolean getPrepareNextFile() {
		return prepareNextFile;
	}
	public void setPrepareNextFile(Boolean prepareNextFile) {
		this.prepareNextFile = prepareNextFile;
	}
	public Boolean getChunked() {
		return chunked;
	}
	public void setChunked(Boolean chunked) {
		this.chunked = chunked;
	}
	public int getChunkSize() {
		return chunkSize;
	}
	public void setChunkSize(int chunkSize) {
		this.chunkSize = chunkSize;
	}
	public int getChunkRetry() {
		return chunkRetry;
	}
	public void setChunkRetry(int chunkRetry) {
		this.chunkRetry = chunkRetry;
	}
	public int getThreads() {
		return threads;
	}
	public void setThreads(int threads) {
		this.threads = threads;
	}
	public String getFormData() {
		return formData;
	}
	public void setFormData(String formData) {
		this.formData = formData;
	}
	public String getFileVal() {
		return fileVal;
	}
	public void setFileVal(String fileVal) {
		this.fileVal = fileVal;
	}
	public String getMethod() {
		return method;
	}
	public void setMethod(String method) {
		this.method = method;
	}
	public Boolean getSendAsBinary() {
		return sendAsBinary;
	}
	public void setSendAsBinary(Boolean sendAsBinary) {
		this.sendAsBinary = sendAsBinary;
	}
	public int getFileNumLimit() {
		return fileNumLimit;
	}
	public void setFileNumLimit(int fileNumLimit) {
		this.fileNumLimit = fileNumLimit;
	}
	public int getFileSizeLimit() {
		return fileSizeLimit;
	}
	public void setFileSizeLimit(int fileSizeLimit) {
		this.fileSizeLimit = fileSizeLimit;
	}
	public int getFileSingleSizeLimit() {
		return fileSingleSizeLimit;
	}
	public void setFileSingleSizeLimit(int fileSingleSizeLimit) {
		this.fileSingleSizeLimit = fileSingleSizeLimit;
	}
	public Boolean getDuplicate() {
		return duplicate;
	}
	public void setDuplicate(Boolean duplicate) {
		this.duplicate = duplicate;
	}
	public String getDisableWidgets() {
		return disableWidgets;
	}
	public void setDisableWidgets(String disableWidgets) {
		this.disableWidgets = disableWidgets;
	}
	public String getThumb() {
		return thumb;
	}
	public void setThumb(String thumb) {
		this.thumb = thumb;
	}
	public String getCompress() {
		return compress;
	}
	public void setCompress(String compress) {
		this.compress = compress;
	}

	public String getIdField() {
		return idField;
	}

	public void setIdField(String idField) {
		this.idField = idField;
	}

	public String getFilepathField() {
		return filepathField;
	}

	public void setFilepathField(String filepathField) {
		this.filepathField = filepathField;
	}

	public String getWebUploadType() {
		return webUploadType;
	}

	public void setWebUploadType(String webUploadType) {
		this.webUploadType = webUploadType;
	}

	public String getSelectFileType() {
		return selectFileType;
	}

	public void setSelectFileType(String selectFileType) {
		this.selectFileType = selectFileType;
	}

	public String getTableName() {
		return tableName;
	}

	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	public String getTableFeildName() {
		return tableFeildName;
	}

	public void setTableFeildName(String tableFeildName) {
		this.tableFeildName = tableFeildName;
	}

	public String getWebName() {
		return webName;
	}

	public void setWebName(String webName) {
		this.webName = webName;
	}

	public String getDndAccept() {
		return dndAccept;
	}

	public void setDndAccept(String dndAccept) {
		this.dndAccept = dndAccept;
	}

	public String getFilesQueued() {
		return filesQueued;
	}

	public void setFilesQueued(String filesQueued) {
		this.filesQueued = filesQueued;
	}

	public String getBeforeFileQueued() {
		return beforeFileQueued;
	}

	public void setBeforeFileQueued(String beforeFileQueued) {
		this.beforeFileQueued = beforeFileQueued;
	}

	public String getUploadSuccess() {
		return uploadSuccess;
	}

	public void setUploadSuccess(String uploadSuccess) {
		this.uploadSuccess = uploadSuccess;
	}

	public String getUploadError() {
		return uploadError;
	}

	public void setUploadError(String uploadError) {
		this.uploadError = uploadError;
	}

	public String getUploadComplete() {
		return uploadComplete;
	}

	public void setUploadComplete(String uploadComplete) {
		this.uploadComplete = uploadComplete;
	}

	public String getUploadStart() {
		return uploadStart;
	}

	public void setUploadStart(String uploadStart) {
		this.uploadStart = uploadStart;
	}

	public String getUploadBeforeSend() {
		return uploadBeforeSend;
	}

	public void setUploadBeforeSend(String uploadBeforeSend) {
		this.uploadBeforeSend = uploadBeforeSend;
	}

	public String getUploadAccept() {
		return uploadAccept;
	}

	public void setUploadAccept(String uploadAccept) {
		this.uploadAccept = uploadAccept;
	}
	
	
}
